package example.telosys.persistence.sqlscript ;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.telosys.common.TelosysException;
import org.telosys.persistence.database.DatabaseManager;
import org.telosys.persistence.sql.SqlScriptExecutor;

import test.telosys.persistence.TestCommons;

public class SqlScriptUtil {

	public static String getSqlScriptFileName(String sqlFile) 
	{
		return "D:/workspaces/wks37-telosys-framework-V2/telosys-persistence/" 
				+ "src/test/resources/"
				+ "sqlscripts/"
				+ sqlFile ;
	}
	
	/**
	 * @param args
	 */
	public static void execute(String sqlFile) throws TelosysException
	{
		TestCommons.init();
		
		Connection connection = DatabaseManager.getConnection();
		
		//String sqlFilePath = TestCommons.getFullFileName("sql/" + sqlFile );
		String sqlFilePath = getSqlScriptFileName(sqlFile);
		
		SqlScriptExecutor r = new SqlScriptExecutor(connection, SqlScriptExecutor.AUTOCOMMIT );
		
		try {
			r.executeScript(sqlFilePath);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		finally {
			
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
